package svc;

import java.util.HashMap;

import utils.Utility;

import dao.LoginDao;

public class LoginSvc {
	private LoginDao dao = new LoginDao();
	
	
	/**
	 * Takes in username and password
	 * return userId	if username & password exists and match
	 * return -1		in all other cases
	 */
	public int isUidPwdValid(String username, String password){
		int userId = -1;
		HashMap<String, String> uidPwdMap = dao.getUidPwds();
		
		if(uidPwdMap.containsKey(username)){
			if(uidPwdMap.get(username).equals(Utility.encodePassword(password))){
				userId = dao.getUserIdFromEmail(username);
			}
		}
		return userId;
	}
	
	
	/**
	 * Takes in email
	 * return true if username exists
	 * return false otherwise
	 */
	public boolean doesUserExists(String email) {
		boolean result = false;
		HashMap<String, String> uidPwdMap = dao.getUidPwds();
		if(uidPwdMap.containsKey(email)){
			result = true;
		}
		return result;
	}
	
	
	/**
	 * Takes in new user details
	 * return userId (newly generated) if user registered successfully
	 * return -1 if there was any error while registering user
	 */
	public int registerUser(String name, String email, String password) {
		return dao.registerUser(name, email, password);
	}
	
	
	/**
	 * Takes in email and new password
	 * return true if password updated successfully
	 * return false otherwise
	 */
	public boolean updatePassword(String email, String password){
		return dao.updatePassword(email, password);
	}


	/**
	 * Takes in userId
	 * return Name
	 */
	public String getName(int userId) {
		return dao.getName(userId);
	}
}
